package com.xebest.web.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xebest.web.form.XeVenOrderEnteringLogisticsForm;
import com.xebest.web.mapper.read.XeVenOrderEnteringLogisticsReadMapper;
import com.xebest.web.mapper.write.XeVenOrderEnteringLogisticsWriterMapper;
import com.xebest.web.model.XeVenOrderLogisticsDetail;
import com.xebest.web.service.XeVenOrderEnteringLogisticsService;
import com.xebest.web.view.XeVenOrderEnteringLogisticsDetailView;
import com.xebest.web.view.XeVenOrderEnteringLogisticsView;

@Service
public class XeVenOrderEnteringLogisticsServiceImpl implements XeVenOrderEnteringLogisticsService {

	
	@Autowired
	private XeVenOrderEnteringLogisticsReadMapper enteringLogisticsReadMapper ;
	
	@Autowired
	private XeVenOrderEnteringLogisticsWriterMapper  enteringLogisticsWriterMapper;
	
	
	/**
	 * 查询列表
	 * @author 袁世林
	 * @param form
	 * @return List<XeVenOrderEnteringLogisticsView>
	 */
	@Override
	public List<XeVenOrderEnteringLogisticsView> queryEnteringLogisticsList(XeVenOrderEnteringLogisticsForm form) {
		
		return enteringLogisticsReadMapper.queryEnteringLogisticsList(form) ;
	}

	/**
	 * 根据订单号查询物流相关信息
	 * @author 袁世林
	 * @param form
	 * @return XeVenOrderEnteringLogisticsView
	 */
	@Override
	public XeVenOrderEnteringLogisticsView queryOrderLogisticsInfo(XeVenOrderEnteringLogisticsForm form) {
		
		return enteringLogisticsReadMapper.queryOrderLogisticsInfo(form);
	}
	
	/**
	 * 保存新建的物流信息
	 * @author 袁世林
	 * @param formList
	 */
	@Override
	@Transactional(value = "writeTransactionManager")
	public void saveEnteringLogistics(XeVenOrderEnteringLogisticsForm form , List<XeVenOrderEnteringLogisticsForm> formList) {
		
		//插入主表
		enteringLogisticsWriterMapper.saveEnteringLogistics(form) ;
		
			if (formList != null)
			{
				for (XeVenOrderEnteringLogisticsForm f : formList)
				{
					f.setId(form.getId());
				}
			}
			
			enteringLogisticsWriterMapper.saveEnteringDetailLogistics(formList) ;
		
	}

	/**
	 * 根据物流单号查看详情
	 * @author 袁世林
	 * @param formList
	 */
	@Override
	public XeVenOrderEnteringLogisticsView queryEnteringLogisticsById(XeVenOrderEnteringLogisticsForm form) {
		
		XeVenOrderEnteringLogisticsView orderLogisticsInfo = enteringLogisticsReadMapper.queryOrderLogisticsInfoById(form) ;
		
		List<XeVenOrderEnteringLogisticsDetailView> detailView = enteringLogisticsReadMapper.queryEnteringLogisticsDetailList(form) ;
		
		if (orderLogisticsInfo != null)
		{
			orderLogisticsInfo.setDetailView(detailView);
		}
		return orderLogisticsInfo ;
	}

	/**
	 * 编辑新建的物流信息
	 * @author 袁世林
	 * @param formList
	 */
	@Override
	@Transactional(value = "writeTransactionManager")
	public void editEnteringLogistics(XeVenOrderEnteringLogisticsForm form , List<XeVenOrderEnteringLogisticsForm> formList) {
		
		enteringLogisticsWriterMapper.deleteEnteringLogisticsById(form.getId()) ;
		
		for (XeVenOrderEnteringLogisticsForm f : formList)
		{
			f.setId(form.getId());
		}
		enteringLogisticsWriterMapper.saveEnteringDetailLogistics(formList) ;

	}

	/**
	 * 作废新建的物流信息
	 * @author 袁世林
	 * @param logsCode
	 */
	@Override
	@Transactional(value = "writeTransactionManager")
	public void cancelEnteringLogistics(Integer id) {
		
		enteringLogisticsWriterMapper.updateOrderLogisticsCreateLogisticType(id , 2 , null) ;
	}

	/**
	 * 删除新建的物流信息
	 * @author 袁世林
	 * @param logsCode
	 */
	@Override
	@Transactional(value = "writeTransactionManager")
	public void deletelEnteringLogistics(Integer id) {
		
		enteringLogisticsWriterMapper.updateOrderLogisticsCreateLogisticType(id , 0 , null) ;
	}
	
	/**
	 * 判断该订单号是否已经创建物流 :0.自动录入 1.手工录入生效 2.手工录入未生效',如果是1，则不能创建
	 * @author 袁世林
	 * @param orderId
	 * @return createLogisticType
	 */
	@Override
	public Integer queryOrderLogisticsCreateLogisticType(String orderId) {
		return enteringLogisticsReadMapper.queryOrderLogisticsCreateLogisticType(orderId) ;
	}

	/**
	 * 查询新建的物流单详情，让商城移动端展示
	 * @author 袁世林
	 * @param orderId
	 * @return List<XeVenOrderLogisticsDetail>
	 */
	@Override
	public List<XeVenOrderLogisticsDetail> queryEnteringLogisticsDetailListByOrderId(String orderId) {
		
		return enteringLogisticsReadMapper.queryEnteringLogisticsDetailListByOrderId(orderId);
	}

	/**
	 * 查询最近的物流单详情，让商城移动端展示
	 * @author 袁世林
	 * @param userId
	 * @return XeVenOrderLogisticsDetail
	 */
	@Override
	public XeVenOrderLogisticsDetail getLatestLogistics(String userId) {
		
		return enteringLogisticsReadMapper.getLatestLogistics(userId);
	}
	
}
